home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-05-03 | 8.8 KB | 270 lines | [TEXT/ZBAS] |
- '*************************************************
- '***************** Apple Events ******************
- '*************************************************
- '* by George Ritchie (AO: GRITCHIE) May 2, 1992 *
- '*************************************************
- '* written in FUTURE BASIC™ ( © Zedcor, Inc. 1992) *
- '*************************************************
- '* This program implements the four
- '* Apple Events (open application, open document,
- '* print document, quit) required of "System-7
- '* friendly" applications. A one-line text
- '* document (AEvents.doc) with the appropriate creator
- '* type is included with this program. Send the
- '* application an "Open" or "Print" event for this
- '* document from the Finder - the document will be
- '* opened and its contents displayed in the
- '* application's window.
- '*************************************************
- '* The "SIZE -1" resource int the AEVENTS.RSRC
- '* file has the "High Level Event Aware" flag
- '* set - this is required for getting the Finder
- '* to send the Apple Events to the application.
- '*************************************************
- '* The "include" file contains assembly-language
- '* versions of the system function calls required
- '* for handling Apple Events and opening files
- '* using System-7 specific routines. As of this
- '* writing, these calls are not part of the
- '* standard Future Basic library.
- '*************************************************
- '* You must BUILD the application and run it
- '* separately from Future Basic. (Otherwise you won't
- '* be able to receive Apple Events!)
- '*************************************************
- '* Please don't even think about running this
- '* without System 7! A real program must use Gestalt
- '* to check for the existence of the PPC toolbox
- '* (standard part of Sys. 7) before using Apple Events.
- '*************************************************
- '* I want to extend this program example so that
- '* it can send and receive other types of Apple Events
- '* (including custom-defined ones) that will execute
- '* menu commands, button pushes, send data etc.
- '* The objective is to produce applications that are
- '* scriptable using the Apple-Event protocol.
- '* Any suggestions or advice from others on using
- '* Apple Events for scripting would be most welcome!
- '*************************************************
- '* credits:
- '* The FUTURE BASIC compiler was created by Andrew Gariepy. *
- '* Information on Apple Events was obtained from:
- '* Inside Macintosh Vol. 6 (Apple, 1991)
- '* Programming for System 7 (Addison Wesley, 1991)
- '* Macintosh Programmers Workshop 3.2 (Apple, 1991)
- '**************************************************
-
- DEFSTR LONG
- RESOURCES "AEVENTS.RSRC","TEXTpAr3"
- INCLUDE "AEVENTS.INCL"
-
- _nil=0
- _LocationNameRecLen=136
- _PortInfoRecLen=82
- _PPCPortRecLen=80
- _ioMisc=28
-
- gQuit=0
-
- '* class and types of events we will handle *
- '* all required evnents are members of the Core Event class *
- kCoreEventClass&=CVI("aevt")
- kAEOpenApplication&=CVI("oapp")
- kAEOpenDocuments&=CVI("odoc")
- kAEQuitApplication&=CVI("quit")
- kAEPrintDocuments&=CVI("pdoc")
-
- '* parameters are passed directly in these events *
- keyDirectObject&=CVI("----")
-
- '* define data-structure types that will be used *
- typeAEList&=CVI("list")
- typeAERecord&=CVI("reco")
- typeFSS&=CVI("fss ")
-
- ' * tagged data, the standard AppleEvent data type *
- DIM RECORD AEDesc
- DIM AEdescriptorType&: '* data type specification *
- DIM AEdataHandle&: '* handle to the data *
- DIM END RECORD _AEDescRecLen
-
- '* other data structures that we need: *
-
- DIM PPCPortRec. _PPCPortRecLen
- DIM theLocation. _LocationNameRecLen
- DIM thePortInfo. _PortInfoRecLen
- DIM theAEDesc. _AEDescRecLen
-
- DIM RECORD TargetID
- DIM sessionID&
- DIM TargetIDname. _PPCPortRecLen
- DIM TargetIDlocation. _LocationNameRecLen
- DIM TargetIDrecvrName. _PPCPortRecLen
- DIM END RECORD
-
- DIM RECORD gTheAddress
- DIM gTheAdddescriptorType&
- DIM gTheAdddataHandle&
- DIM END RECORD
-
- GOTO "START": '* skip over functions *
-
- LONG FN GetAEDocumentInfo(AE_messageinPTR&,PrintDoc)
- '* extract the required info from Apple Event to open a document
- DIM itemsInList&,index&,keywd&,returnedType&,actualSize&
-
- '* System-7 file info structure: *
- DIM RECORD myFSS
- DIM myFSSvRefNum%
- DIM myFSSparID&
- DIM 63 myFSSname$
- DIM END RECORD _myFSSRecLen
-
- '* list of documents to be opened: *
- DIM docList. _AEDescRecLen
-
- myErr=FN AEGetParamDesc(AE_messageinPTR&,keyDirectObject&,typeAEList&,@docList)
-
- LONG IF myErr
- BEEP
- XELSE
- '* see if all parameters required for this Apple Event are actually there *
- myErr=FN MyGotRequiredParams(AE_messageinPTR&)
- END IF
-
- LONG IF myErr=0
-
- '* how many document were requested to be opened? *
- myErr=FN AECountItems(@docList,@itemsInList&)
-
- LONG IF myErr
- BEEP
- XELSE
- PRINT "ITEMS IN LIST=";itemsInList&
- FOR index&=1 TO itemsInList&
- '* extract info on a particular document *
- myErr=FN AEGetNthPtr(@docList,index&,typeFSS&,@keywd&,@returnedType&,@myFSS,_myFSSRecLen,@actualSize&)
- LONG IF myErr
- BEEP
- XELSE
- '* vRefNum% is volume no. - it does not identify a folder *
- PRINT "vRefNum%=";myFSSvRefNum%;", FILENAME=";myFSSname$
-
- '* Following function opens our document, given
- '* the FSS record for it.
- myErr=FN OpenDoc(@myFSS,@toDataHandl&)
-
- LONG IF toDataHandl&
- '* we opened and red the file, so show its contents *
- A$=""
- L=FN GETHANDLESIZE(toDataHandl&)
- IF L>32 THEN L=32
- BLOCKMOVE PEEK LONG(toDataHandl&),@A$+1,L
- POKE @A$,L
- PRINT A$
- IF PrintDoc THEN PRINT "CALL PRINT ROUTINE HERE"
- X=FN DISPOSHANDLE(toDataHandl&)
- END IF
-
- END IF
-
- NEXT index&
-
- END IF
- END IF
- END FN
-
- LONG FN InitAEStuff
- aevtErr = FN AEInstallEventHandler(kCoreEventClass&,kAEOpenApplication&,LINE "AEOpenHandler",0,_false)
- aevtErr = FN AEInstallEventHandler(kCoreEventClass&,kAEOpenDocuments&,LINE "AEOpenDocHandler",1,_false)
- aevtErr = FN AEInstallEventHandler(kCoreEventClass&,kAEQuitApplication&,LINE "AEQuitHandler",2,_false)
- aevtErr = FN AEInstallEventHandler(kCoreEventClass&,kAEPrintDocuments&,LINE "AEPrintHandler",3,_false)
- END FN
-
- LOCAL FN DoHighLevel(AERecordPtr&)
- FN AEProcessAppleEvent(AERecordPtr&)
- END FN
-
- "START"
- WINDOW 1
-
- '* install the Apple Event handlers *
- FN InitAEStuff
-
- ON EVENT GOSUB "EVENT"
- ON DIALOG GOSUB "DIALOG"
- ON BREAK GOSUB "REMOVE_EVENT_HANDLERS"
-
- DO
- EVENT ON:DIALOG ON:BREAK ON
- EVENT OFF:DIALOG OFF:BREAK OFF
- UNTIL gQuit
-
- GOSUB "REMOVE_EVENT_HANDLERS"
-
- END
-
-
- "REMOVE_EVENT_HANDLERS"
- '* before quitting, remove Apple Event handlers we had installed *
- aevtErr = FN AERemoveEventHandler(kCoreEventClass&,kAEOpenApplication&,AEOpenHandler&,_false)
- aevtErr = FN AERemoveEventHandler(kCoreEventClass&,kAEOpenDocuments&,AEOpenDocHandler&,_false)
- aevtErr = FN AERemoveEventHandler(kCoreEventClass&,kAEQuitApplication&,AEQuitHandler&,_false)
- aevtErr = FN AERemoveEventHandler(kCoreEventClass&,kAEPrintDocuments&,AEPrintHandler&,_false)
- gQuit=-1
- RETURN
-
- "EVENT"
- '* check for high-level event before letting Future Basic handle the event *
- LONG IF PEEK WORD(EVENT)=_kHighLevelEvent
- FN DoHighLevel(EVENT)
- POKE WORD (EVENT),0: '* clear the event, we handled it *
- CURSOR 0
- END IF
- RETURN
-
- "DIALOG"
- '* get low-level event information *
- DIALOG_ZERO=DIALOG(0)
- DIALOG_ONE=DIALOG(DZERO)
- RETURN
-
- '* The following PROCs automatically take care of setting up
- '* and restoring CPU registers:
-
- "AEOpenHandler"
- '* Open Application event.*
- ENTERPROC (AE_messageinPTR&,AE_replyPTR&,refIn&)
- AE_replyPTR&=0: '* no reply to this event *
- PRINT "open appl EVENT"
- returnMessage%=_noErr
- EXITPROC=returnMessage%
-
- "AEOpenDocHandler"
- '* Open Document event.*
- ENTERPROC (AE_messageinPTR&,AE_replyPTR&,refIn&)
- AE_replyPTR&=0
- FN GetAEDocumentInfo(AE_messageinPTR&,0)
- EXITPROC=myErr
-
- "AEPrintHandler"
- '* Print Document event.*
- ENTERPROC (AE_messageinPTR&,AE_replyPTR&,refIn&)
- AE_replyPTR&=0
- FN GetAEDocumentInfo(AE_messageinPTR&,1)
- gPrintDoc=0
- EXITPROC=myErr
-
-
- "AEQuitHandler"
- '* Standard Quit event handler, to handle a Quit event from the Finder, for example. *
- ENTERPROC (AE_messageinPTR&,AE_replyPTR&,refIn&)
- AE_replyPTR&=0
- PRINT "quit EVENT"
- DELAY 1000
- '* never quit here - return to main loop before quitting! *
- gQuit = _true
- returnMessage%= _noErr
- EXITPROC=returnMessage%
-
-